package com.odriver.neulearn.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.odriver.neulearn.Bean.DetailCourseBean;
import com.odriver.neulearn.Bean.MenuBean;
import com.odriver.neulearn.R;

import java.util.List;

/**
 * Created by yeruyi on 2016/11/28.
 */

public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {

    private List<DetailCourseBean.PostStageResListBean> data;
    private Context context;
    private LayoutInflater inflate;

    public MyExpandableListViewAdapter(Context context, List<DetailCourseBean.PostStageResListBean> data) {
        this.data = data;
        this.context = context;
        inflate = LayoutInflater.from(context);
    }

    //  获得某个父项的某个子项
    @Override
    public Object getChild(int parentPos, int childPos) {
        return data.get(parentPos).getChildStageList().get(childPos);
    }

    //  获得父项的数量
    @Override
    public int getGroupCount() {
        return data.size();
    }

    //  获得某个父项的子项数目
    @Override
    public int getChildrenCount(int parentPos) {
        return data.get(parentPos).getChildStageList().get(0).getChildStageResList().size();
    }

    //  获得某个父项
    @Override
    public Object getGroup(int parentPos) {
        return data.get(parentPos);
    }

    //  获得某个父项的id
    @Override
    public long getGroupId(int parentPos) {
        return parentPos;
    }

    //  获得某个父项的某个子项的id
    @Override
    public long getChildId(int parentPos, int childPos) {
        return childPos;
    }

    //  按函数的名字来理解应该是是否具有稳定的id，这个方法目前一直都是返回false，没有去改动过
    @Override
    public boolean hasStableIds() {
        return false;
    }

    //  获得父项显示的view
    @Override
    public View getGroupView(int parentPos, boolean b, View convertView, ViewGroup viewGroup) {
        ViewHolder_Parent viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder_Parent();
            convertView = inflate.inflate(R.layout.item_detail_parent, null);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.idp_detail_parent_topic);
            viewHolder.topic = (TextView) convertView.findViewById(R.id.idp_detail_parent_topic_detail);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder_Parent) convertView.getTag();
        }

        viewHolder.textView.setText(data.get(parentPos).getParentStageName());
        viewHolder.topic.setText(data.get(parentPos).getChildStageList().get(0).getStageName());
        return convertView;

    }

    //  获得子项显示的view
    @Override
    public View getChildView(int parentPos, int childPos, boolean b, View convertView, ViewGroup viewGroup) {
        ViewHolder_Child viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder_Child();
            convertView = inflate.inflate(R.layout.item_detail_child, null);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.idp_detail_child_topic);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder_Child) convertView.getTag();
        }
        viewHolder.textView.setText(data.get(parentPos).getChildStageList().
                get(0).getChildStageResList().get(childPos).getResName());
        return convertView;
    }

    //  子项是否可选中，如果需要设置子项的点击事件，需要返回true
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class ViewHolder_Parent {
        TextView textView,topic;
    }
    class ViewHolder_Child {
        TextView textView;
    }

    

}
